package com.llkj.qingnianguoxue.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.MyPraisesAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.HotRankingBean;
import com.llkj.qingnianguoxue.home.VideoDetailActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 我赞过的，我的评论
 */
public class MyPraiseActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private static final int REQUEST_VIDEO_CODE = 9980;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvPraises;
    private MyPraisesAdapter praisesAdapter;
    private List<HotRankingBean.RankingBean> mPraiseData;
    private int mFromClass;//1点赞，2评论
    private int totalSize, mPage, mCurrentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_praise);
        setTitle("我赞过的", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        mFromClass = bundle.getInt("from_class", 1);

        initView();
        initListener();

        if (mFromClass == 2) {//我的评论
            tvTitle.setText("我的评论");
        }

        loadWaitDialog();
        onRefresh();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvPraises = (RecyclerView) findViewById(R.id.recyclerView);
        lvPraises.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        int padding = DisplayUtil.dip2px(this, 10);
        lvPraises.setPadding(padding, 0, padding, padding);

        mPraiseData = new ArrayList<>();
        praisesAdapter = new MyPraisesAdapter(R.layout.item_my_praises, mPraiseData);
        lvPraises.setAdapter(praisesAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        praisesAdapter.setOnLoadMoreListener(this);

        lvPraises.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                HotRankingBean.RankingBean rankingBean = mPraiseData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("id", rankingBean.getContent_id());
                startActivityForResult(VideoDetailActivity.class, bundle, REQUEST_VIDEO_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_VIDEO_CODE) {

            }
        }
    }

    @Override
    public void onLoadMore() {
        mPage++;
        if (mFromClass == 2) {
            getLeaveData();
        } else {
            getPraiseData();
        }
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        if (mFromClass == 2) {
            getLeaveData();
        } else {
            getPraiseData();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, praisesAdapter, null, lvPraises, totalSize);
    }

    /**
     * 8.14我赞过的数据
     */
    private void getPraiseData() {
        HttpMethod.userPraise(this, this, String.valueOf(mPage));
    }

    /**
     * 8.15我的评论
     */
    private void getLeaveData() {
        HttpMethod.userLeave(this, this, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);

        if (method == HttpMethod.HTTP_USER_PRAISE || method == HttpMethod.HTTP_USER_LEAVE) {//8.14我赞过的,8.15我的评论
            HotRankingBean recordListBean = GsonUtil.GsonToBean(data, HotRankingBean.class);
            if (recordListBean != null) {
                totalSize = Integer.parseInt(recordListBean.getNum());
                if (mPage == 1) {
                    mPraiseData.clear();
                    LoadDataHelper.refreshComplete(praisesAdapter, mPraiseData, refreshLayout, lvPraises);
                } else {
                    praisesAdapter.setEnableLoadMore(true);
                    praisesAdapter.loadMoreComplete();
                }
                try {
                    if (mFromClass == 2) {
                        mPraiseData.addAll(recordListBean.getLeave());
                    } else {
                        mPraiseData.addAll(recordListBean.getPraise());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                praisesAdapter.notifyDataSetChanged();
            }
            if(mPraiseData.size()==0){
                if (mFromClass == 2) {//我的评论
                    ToastUtil.makeLongText(this,getString(R.string.empty_comment_note));
                } else {//我赞的
                    ToastUtil.makeLongText(this,getString(R.string.empty_praise_note));
                }
            }

        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        if(mPraiseData.size()==0){
            if (mFromClass == 2) {//我的评论
                ToastUtil.makeLongText(this,getString(R.string.empty_comment_note));
            } else {//我赞的
                ToastUtil.makeLongText(this,getString(R.string.empty_praise_note));
            }
        }
    }
}
